var apitool = require('../../sys/apitool.js')
Component({
  options: {
    addGlobalClass: true,
    multipleSlots: true
  },
  /**
   * 组件的属性列表
   */
  properties: {
    // ishow: { // 属性名
    //   type: Boolean,
    //   value: true
    // },
    themcolor: Object
  },

  /**
   * 组件的初始数据
   */
  data: {
    adding:false,
    fileList: [],
    cidshow: false,
    typrshow: true,
    typeactionsdata: [{
        id: 1,
        name: '我要退款（无需退货）'
      },
      {
        id: 2,
        name: '我要退货退款'
      }
    ],
    xcmodel: null,
    actionmodel: {
      show: false,
      title: "",
      data: []
    },
    ishow: false,
    actionsdata: [{
        name: '不想要了'
      },
      {
        name: '描述与商品不符'
      },
      {
        name: '地址填错'
      },
      {
        name: '多拍，错拍'
      },
      {
        name: '其它'
      }
    ]
  },
  ready(){
    var app=getApp()
    if (app.globalData.configdata) {
     
      if (app.globalData.configdata.yihuo) {
        this.setData({
          yihuoconfig: app.globalData.configdata.yihuo
        })
      }
    }
  },
  /**
   * 组件的方法列表
   */
  methods: {
    empmodel() {
      var newmodel = {
        ordernum: '',
        strtype: '',
        reason: '', //原因
        imglist: [],
        remark: "" //备注
      }
      return newmodel

    },

    showmodel: function (ordernum, strtype, orderstatus) {
      var updatemodel = {};
      if (!strtype) {
        strtype = "order"
      }
      var xcmodel = this.data.xcmodel
      if (xcmodel && xcmodel.ordernum == ordernum && xcmodel.strtype == strtype) {

      } else {
        xcmodel = this.empmodel()
        xcmodel['ordernum'] = ordernum
        xcmodel['strtype'] = strtype

        updatemodel["xcmodel"] = xcmodel
        updatemodel["fileList"] = []
      }

      var actionsdata = []
      if (orderstatus == 1) {
        actionsdata = [{
            id: 1,
            name: "商品无货"
          },
          {
            id: 2,
            name: "发货时间问题"
          },
          {
            id: 3,
            name: "不想要了"
          },
          {
            id: 15,
            name: "	其他"
          },
        ]
      } else {
        actionsdata = [{
            id: 5,
            name: "地址信息填写错误"
          },
          {
            id: 6,
            name: "买多/买错/不想要了"
          },
          {
            id: 7,
            name: "商品损坏/包装脏污"
          },
          {
            id: 8,
            name: "少/错商品/与页面描述不符"
          },
          {
            id: 9,
            name: "无效的物流单号"
          },
          {
            id: 10,
            name: "物流超72小时停滞"
          },
          {
            id: 11,
            name: "快递无法送到指定地点"
          },
          {
            id: 12,
            name: "显示签收但未收到商品"
          },
          {
            id: 14,
            name: "	质量问题"
          },
          {
            id: 15,
            name: "	其他"
          },
        ]
      }
      updatemodel["actionsdata"] = actionsdata

      updatemodel["ishow"] = true
      this.setData(updatemodel)
    },
    inputdata: function (ev) {
      var field = ev.currentTarget.dataset.field;
      var upatemodel = {}
      upatemodel['xcmodel.' + field] = ev.detail
      this.setData(upatemodel)
    },
    showPopup: function (e) {
      var key = e.currentTarget.dataset.key
      var model = {}
      if (key == 'type') {
        model = {
          show: true,
          key: key,
          title: "请选择退款类型",
          data: [{
              id: 1,
              name: '我要退款（无需退货）'
            },
            {
              id: 2,
              name: '我要退货退款'
            }
          ]
        }
      } else {
        model = {
          show: true,
          key: key,
          title: "请选择退款原因",
          data: this.data.actionsdata
        }

      }

      this.setData({
        actionmodel: model
      })


      // this.setData({cidshow:true})
    },
    onClose: function () {
      this.setData({
        'actionmodel.show': false
      })

    },
    onSelect: function (ev) {
      var upatemodel = {}
   
      upatemodel['actionmodel.show'] = false

      if (this.data.actionmodel.key == 'type') {
        upatemodel['xcmodel.type'] = ev.detail.id

      } else {
        upatemodel['xcmodel.reasonid'] = ev.detail.id

        upatemodel['xcmodel.reason'] = ev.detail.name

      }
      this.setData(upatemodel)
    },
    ontypeClose: function () {
      this.setData({
        typeshow: false
      })

    },
    ontypeSelect: function (ev) {
      var upatemodel = {}
      upatemodel['xcmodel.type'] = ev.detail.id
      this.setData(upatemodel)
    },
    Backfunc: function () {
      this.setData({
        ishow: false
      })
    },
    valide() {
      var error = []
      if (!this.data.xcmodel.type) {
        error.push("请选择退款类型")
      }
      if (!this.data.xcmodel.reason) {
        error.push("请选择退款原因")
      }
      if (!this.data.xcmodel.remark) {
        error.push("请说明退款原因")
      }
      if (error.length > 0) {
        error = error.toString()
     
        wx.showModal({
          title: "提示",
          content: error,
        })
        return false
      } else {
        return true
      }


    },

    optool(ev) {
      var dataset = ev.currentTarget.dataset

      var that = this;
      switch (dataset.op) {
        case "add": {
          var res = this.valide()
          if (res) {
            that.setData({
              adding:true
            })
            var urlpara = {
              do: 'apply_refund',
              'op': dataset.op
            }
            apitool.post(urlpara, {
              xc: JSON.stringify(this.data.xcmodel)
            }).then(res => {
              that.setData({
                adding:false
              })
              if (res["status"] == 1) {
                that.triggerEvent("aftersucess", this.data.xcmodel)
                this.setData({
                  ishow: false
                })
                wx.showToast({
                  icon: 'success',
                  title: "操作成功"
                })
              } else {
                wx.showToast({
                  title: res.message,
                })
              }
            })
          }

          break;
        }
      }
    },

    savefilefunc(file) {
      var app = getApp()
      var that = this
      app.toolv2.uploadFile(file.path).then(function (item) {
        var fileList = that.data.fileList;
        fileList.push({
          ...file,
          url: item.obj.fullpath
        });
        that.setData({
          fileList
        });
        that.data.xcmodel.imglist = [];
        fileList.forEach(item => {
          that.data.xcmodel.imglist.push(item.url)
        })
      })

    },
    afterRead(event) {

      var that = this
      const {
        file
      } = event.detail
      if (file.constructor == Array) {
        file.forEach(item => {
          that.savefilefunc(item)
        })
      } else {
        that.savefilefunc(file)
      }
    }
  }
})